<!--script type="text/javascript" src="<?php echo base_url(); ?>static/select2/select2.min.js" /></script>
<script type="text/javascript" src="<?php echo base_url(); ?>static/js/select.js" /></script-->

<form method="post" id="formuser" action="" enctype="multipart/form-data" class="form-horizontal" role="form" onsubmit="return cekDulu(this);">
	<div class="col-md-offset-1">
		<div class="col-md-11">
			<div class="panel-body well">
				<h4 class="panel-title"><b class="caret"></b><b> Form - <?php echo $status_id; ?> Contract</b></h4>
				<div class="col-md-6" style="padding-top: 20px;">
					<div class="form-group" id="Vendor">
						<label class="col-md-4"><p class="text-left">Vendor Name<span style="color:red;">*</span></p></label>
						<div class="col-md-7">
							<select id="vendor_id" class="form-control input-sm required">
								<option value="0">-- select --</option>
							<?php 
								foreach($vendor_select as $vendor_select => $key)
								{
							?>
									<option value="<?php echo $key['VENDOR_ID'] ?>"<?php echo (strtoupper($detail_data['VENDOR_ID']) == $key['VENDOR_ID']) ? 'selected' : ''; ?>><?php echo $key['VENDOR_NAME'] ?></option>
							<?php 
								} 								
							?>							
							</select>
							<!--input type="text" id="vendor_id<?php //echo $i;?>" value="<?php //echo $vendor_select['VENDOR_ID'] ?>" class="select2 form-control input-sm"/-->														
						</div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label for="l_doc_no" class="control-label">Contract No<span style="color:red;">*</span></label></div>
						<div class="col-md-7"><input type="text" class="form-control input-sm required" id="doc_no" value="<?php echo $detail_data['DOC_CONTRACT_NO']; ?>"></div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label for="l_doc_title" class="control-label">Contract Title<span style="color:red;">*</span></label></div>
						<div class="col-md-7"><input type="text" class="form-control input-sm required" id="doc_title" value="<?php echo $detail_data['DOC_TITLE']; ?>"></div>
					</div>
					<div class="form-group">
						<div class="col-md-4"><label class="control-label">Period Date<span style="color:red;">*</span></label></div>
						<div class="col-md-5">
							<div class="input-group">
								<span class="input-group-addon"><small>From</small></span> 
								<?php $date1 = date("d/m/Y",strtotime($detail_data['DOC_SDATE'])); ?>
								<input type="text" class="form-control input-sm" autocomplete="off" id="date1" value="<?php echo $date1; ?>">
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-4"></label>
						<div class="col-md-5">
							<div class="input-group">
								<span class="input-group-addon"><small>To</small></span>
								<?php $date2 = date("d/m/Y",strtotime($detail_data['DOC_EDATE'])); ?>
								<input type="text" class="form-control input-sm" autocomplete="off" id="date2" value="<?php echo $date2; ?>">
							</div>
						</div>
					</div>					
					<div class="form-group">
						<div class="col-md-4"><label for="l_price" class="control-label">Price</label></div>
					</div>				
				</div>
				<div class="col-sm-6 col-md-5">
					<div class="form-group">
						<div class="col-md-4"><label for="l_description" class="control-label">Description</label></div>
						<div class="col-md-8">
							<textarea id="description" row="6" cols="50" class="form-control input-sm" maxlength="160"><?php echo $detail_data['DOC_DESCRIPTION']; ?></textarea>
						</div>
					</div>            
					<div class="form-group">
						<div class="col-md-4"><label for="l_upload_file" class="control-label">Upload File</label></div>
						<div class="col-md-7">
							<input type="file" id="userfile" size="20" onchange="myFunction()">
							<input type="hidden" class="form-control input-sm" id="nama_file" value="">
							<input type="hidden" class="form-control input-sm" id="status_id" value="<?php echo $status_id; ?>">
							<input type="hidden" class="form-control input-sm" id="doc_induk" value="<?php echo $detail_data['DOC_INDUK']; ?>">
							<input type="hidden" class="form-control input-sm" id="doc_id" value="<?php echo $detail_data['DOC_ID']; ?>">
						</div>
					</div>
					
					<script>
					function myFunction()
					{
						var x = document.getElementById("userfile");
						var y = document.getElementById("nama_file");
						y.value = x.value;
					}
					</script>
					
					<?php if (!empty($detail_data['DOC_FILE'])) { ?>
					
					<div class="form-group">
						<div class="col-md-5"><label for="l_reminder" class="control-label">Uploaded File</label></div>
						<div class="col-md-6">
							<a href="../upload/<?php echo $detail_data['DOC_FILE']; ?>" target="_blank"><?php echo $detail_data['DOC_FILE']; ?></a>
							<input type="hidden" class="form-control input-sm" id="doc_file" value="<?php echo $detail_data['DOC_FILE']; ?>">							
						</div>
					</div>
					
					<?php } ?>
					
					<div class="form-group">
						<div class="col-md-4"><label for="l_reminder" class="control-label">Reminder</label></div>
						<div class="col-md-3">
							<input type="text" class="form-control input-sm" id="reminder" value="<?php echo $detail_data['DOC_REMINDER']; ?>">							
						</div>
						<div class="col-md-5"><i>Bulan Sebelumnya</i></div>
					</div>
				</div>
				<div id="tablecontent" class="col-md-12" style="padding-top: 0px;">					
					<div class="form-group">
						<div class="table-responsive">
							<table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
								<thead>
									<tr>
										<th class="tblhead" style="width:3%;text-align:center;">No</th>
										<th class="tblhead" style="width:27%;text-align:center;">Type</th>
										<th class="tblhead" style="width:14%;text-align:center;">Satuan</th>
										<th class="tblhead" style="width:10%;text-align:center;">Qty Awal</th>
										<th class="tblhead" style="width:10%;text-align:center;">Qty Akhir</th>
										<th class="tblhead" style="width:10%;text-align:center;">Currency</th>
										<th class="tblhead" style="width:10%;text-align:center;">Price</th>
										<th class="tblhead" style="width:14%;text-align:center;">Penagihan</th>
										<th class="tblhead" style="text-align:center;">Action</th>
									</tr>
								</thead>
								<tbody>
								<?php
									if (empty($table_data)) {
								?>
									<!--tr><td colspan="9" style="text-align:center">No Data Available</td></tr-->
								<?php
									} else {
										foreach ($table_data as $row) {
											if ($row["DET_PRICE_UNIT"]=="US") {$currency = "US$";} else {$currency = "RP";}
											if ($row["DET_PENAGIHAN"]=="BULAN") {$penagihan = "Bulanan";} else {$penagihan = "Tahunan";}
								?>
									<tr>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;"><?php echo $row["RECNUM"]; ?>.</td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $row["TYPE_PRICE_DESC"]; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $row["DET_SATUAN"]; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $row["DET_QTY_AWAL"]; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $row["DET_QTY_AKHIR"]; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $currency; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $row["DET_PRICE"]; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:left;"><?php echo $penagihan; ?></td>
											<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;">
												<button type="button" class="btn btn-warning btn-block" onclick="edit_data_table('<?php echo $row["DET_ID"]; ?>','<?php echo $detail_data['DOC_ID']; ?>','<?php echo $detail_data['DOC_INDUK']; ?>','<?php echo $status_id; ?>');" >Edit</button>
												<input type="hidden" class="form-control input-sm" id="det_id" value="<?php echo $row["DET_ID"]; ?>">
												<input type="hidden" class="form-control input-sm" id="det_type_price" value="<?php echo $row["DET_TYPE_PRICE"]; ?>">
												
											</td>
									</tr>		
								<?php 	
										}
									}
								?>
								
									<tr>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:3%;text-align:center;">+</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:27%;text-align:left;">
											<select id="type" class="form-control input-sm required">
												<option value="0">-- select --</option>
											<?php 
												foreach($type_select as $type_select => $key)
												{
													echo '<option value= '.$key['TYPE_PRICE_ID'].'>'.$key['TYPE_PRICE_DESC'].'</option>';
												};
											?>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:14%;text-align:left;">
											<select id="satuan" class="form-control input-sm required">
												<option value="0">-- select --</option>
											<?php 
												foreach($satuan_select as $satuan_select => $key)
												{
													echo '<option value= '.$key['TYPE_SATUAN_NAME'].'>'.$key['TYPE_SATUAN_NAME'].'</option>';
												};
											?>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="qty_awal" value="0">	
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="qty_akhir" value="0">							
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<select id="currency" class="form-control input-sm">
												<option value="US">US$</option>
												<option value="RP">RP</option>
											</select>
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:10%;text-align:left;">
											<input type="text" class="form-control input-sm required" id="price" value="0">							
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="width:14%;text-align:left;">
											<select id="penagihan" class="form-control input-sm">
												<option value="BULAN">Bulanan</option>
												<option value="TAHUN">Tahunan</option>
											</select>				
										</td>
										<td bgcolor="#FFFFFF" class="tbl-content-c" style="text-align:center;">
											<button type="button" class="btn btn-success btn-block" onclick="insert_data_table();" >Add</button>
										</td>
									</tr>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				<div class="col-sm-8">
					<div class="col-sm-4"><label for="button" class="control-label">&nbsp;</label></div>
					<div class="col-sm-4">
						<button type="submit" class="btn btn-success btn-block"><!--span class="glyphicon glyphicon-saved"></span--> Save <?php echo $status_id; ?></button>
					</div>
					<div class="col-sm-4">
						<button type="button" class="btn btn-default btn-block" onclick="cancel();">
						<!--span class="glyphicon glyphicon-circle-arrow-left"></span--> Cancel</button>
					</div>
				</div>
			</div>            
		</div>
	</div>
</form>	

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() {
			var x = document.getElementById("userfile");
			
			//$tmpname = $_FILES['file']['tmp_name'];
			
			$.post("<?= site_url() ?>setup/update_data_contract", {
                doc_induk: $('#doc_induk').val(),
				doc_id: $('#doc_id').val(),
				status_id: $('#status_id').val(),
				
				vendor_id: $('#vendor_id').val(),
                doc_no: $('#doc_no').val(),
				doc_title: $('#doc_title').val(),
				date1: $('#date1').val(),
				date2: $('#date2').val(),
				description: $('#description').val(),
				reminder: $('#reminder').val(),
				nama_file: $('#nama_file').val(),
				
				filesize: x.size, 
				filetype: x.type,  
				
				type: $('#type').val(),
				satuan: $('#satuan').val(),
				qty_awal: $('#qty_awal').val(),
				qty_akhir: $('#qty_akhir').val(),
				currency: $('#currency').val(),
				price: $('#price').val(),
				penagihan: $('#penagihan').val()
            },
            function(data) {
                window.location = '<?php echo base_url();?>setup/contract';
            });
        }
    });

    $(document).ready(function() {
        $("#formuser").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $.validator.setDefaults({
                    submitHandler: function() {
                        var x = document.getElementById("userfile");												
						
						$.post("<?= site_url() ?>setup/update_data_contract", {
                            doc_induk: $('#doc_induk').val(),
							doc_id: $('#doc_id').val(),
							status_id: $('#status_id').val(),
							
							vendor_id: $('#vendor_id').val(),
							doc_no: $('#doc_no').val(),
							doc_title: $('#doc_title').val(),
							date1: $('#date1').val(),
							date2: $('#date2').val(),
							description: $('#description').val(),
							reminder: $('#reminder').val(),
							nama_file: $('#nama_file').val(),
				
							filesize: x.size, 
							filetype: x.type,  
											
							type: $('#type').val(),
							satuan: $('#satuan').val(),
							qty_awal: $('#qty_awal').val(),
							qty_akhir: $('#qty_akhir').val(),
							currency: $('#currency').val(),
							price: $('#price').val(),
							penagihan: $('#penagihan').val()
                        },
                        function(data) {
                            window.location = '<?php echo base_url();?>setup/contract';
                        });
                    }
                });
            }
        });

        $("#formuser").validate();
    });

    function cancel() {
		$.post("<?= site_url() ?>setup/contract", function(data) {
            window.location = '<?php echo base_url();?>setup/contract';
        });		
    }
	
	function cekDulu(frm) {
		if (frm.elements['vendor_id'].value=="0") {
			frm.elements['vendor_id'].focus();
			alert('Isi Vendor Name nya');
			return false;
		}
		if (frm.elements['doc_no'].value.length==0) {
			frm.elements['doc_no'].focus();
			alert('Isi Contract No nya');
			return false;
		}		
		if (frm.elements['status_id'].value=="Revise") {			
			if (frm.elements['nama_file'].value.length==0) {			
				frm.elements['userfile'].focus();
				alert('Upload File Revise nya');
				return false;
			} else {
				if (!(frm.elements['nama_file'].value == frm.elements['doc_file'].value)) {
					//alert('Upload File Revise nya Harus sama Nama Filenya');
					//return false;
				} 
			}	
		}
	}
	
	function insert_data_table() {
		$.post("<?= site_url() ?>setup/insert_data_table", {
				doc_id    : $('#doc_id').val(), 
				type      : $('#type').val(),  
				satuan    : $('#satuan').val(),  
				qty_awal  : $('#qty_awal').val(), 
				qty_akhir : $('#qty_akhir').val(),  
				currency  : $('#currency').val(),  
				price	  : $('#price').val(),  
				penagihan : $('#penagihan').val()  
			}, 
		function(data) {
            $('#loader').html(data);
        });
	}
	
	function edit_data_table(det_id, doc_id, doc_induk, status_id) {		
        $.post("<?= site_url() ?>setup/edit_data_table", {det_id: det_id, doc_id: doc_id, doc_induk: doc_induk, status_id: status_id}, function(data) {
            $('#loader').html(data);
        });
    }
</script>

<script type="text/javascript">
//	$('#image_button').click(function()
//	{
//		$('#date1').datetimepicker('show'); //support hide, show and destroy command
//	});
//	
//	$('#image_button2').click(function()
//	{
//		$('#date2').datetimepicker('show'); 
//	});

	$('#date1').datetimepicker({
		scrollInput:false,
		closeOnDateSelect:true,
		datepicker:true,
		timepicker:false,
		format:'d/m/Y',
		minDate:'-1970/01/01',
		onGenerate:function( ct ){$(this).find('.xdsoft_date.xdsoft_weekend').addClass('xdsoft_disabled');}
	});
	
	$('#date2').datetimepicker({
		scrollInput:false,
		closeOnDateSelect:true,
		datepicker:true,
		timepicker:false,
		format:'d/m/Y',
		minDate:'-1970/01/01',
		onGenerate:function( ct ){$(this).find('.xdsoft_date.xdsoft_weekend').addClass('xdsoft_disabled');}
	});

//    $(document).ready(function() {
//     	$('#vendor_id').select2({
//	  		placeholder: "Select vendor name",
//			allowClear: true
//		});
//    });
</script>